const util = require('../../../utils/util.js')
const app = getApp()

Page({
  data: {
    CustomBar: app.globalData.CustomBar,
    theme: app.globalData.theme, //全局颜色变量
    config: app.globalData.config,
    page: {
      searchCount: false,
      current: 1,
      size: 10,
      ascs: '', //升序字段
      descs: 'create_time'
    },
    parameter: {},
    loadmore: true,
    goodsAppraises: [],
    tabCur: 0,
    appraisesStatus: [{
      value: '全部',
      key: '1'
    }, {
      value: '好评',
      key: '2'
    }, {
      value: '有图',
      key: '3'
    }]
  },
  onLoad(options) {
    let providerId = options.providerId
    this.setData({
      ['parameter.providerId']: providerId
    })
    app.initPage()
      .then(res => {
        this.loadCountAppraises()
        this.goodsAppraisesPage()
      })
  },
  // 加载评价统计数据
  loadCountAppraises() {
    app.api.providerCountAppraises().then(res => {
      let countAppraises = res.data
      this.setData({
        appraisesStatus: [{
          value: '全部' + (countAppraises.totalNum ? ('（'+countAppraises.totalNum+'）') : ''),
          key: '1'
        }, {
          value: '好评' + (countAppraises.goodNum ? ('（'+countAppraises.goodNum+'）') : ''),
          key: '2'
        }, {
          value: '有图' + (countAppraises.havePicNum ? ('（'+countAppraises.havePicNum+'）') : ''),
          key: '3'
        }]
      })
    })
  },
  goodsAppraisesPage() {
    app.api.goodsAppraisesPage(Object.assign({},
        this.data.page,
        util.filterForm(this.data.parameter)
      ))
      .then(res => {
        let goodsAppraises = res.data.records
        this.setData({
          goodsAppraises: [...this.data.goodsAppraises, ...goodsAppraises]
        })
        if (goodsAppraises.length < this.data.page.size) {
          this.setData({
            loadmore: false
          })
        }
      })
  },
  onReachBottom() {
    if (this.data.loadmore) {
      this.setData({
        ['page.current']: this.data.page.current + 1
      })
      this.goodsAppraisesPage()
    }
  },
  previewImage(e) {
    wx.previewImage({
      urls: e.currentTarget.dataset.url,
      current: e.currentTarget.dataset.url
    });
  },
  tabSelect(e) {
    let dataset = e.currentTarget.dataset
    if (dataset.index != this.data.tabCur) {
      this.setData({
        tabCur: dataset.index,
        ['parameter.queryType']: dataset.key
      })
      this.refresh()
    }
  },
  refresh() {
    this.setData({
      loadmore: true,
      goodsAppraises: [],
      ['page.current']: 1
    })
    this.goodsAppraisesPage()
  },
})